package com.rmt.www.daoImpl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.rmt.www.dao.RequirementArtifactRequestDao;
import com.rmt.www.vo.FeatureArtifactRequest;
import com.rmt.www.vo.RequirementArtifactRequest;

public class RequirementArtifactRequestDaoImpl implements RequirementArtifactRequestDao {

	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public void deleteRequirementArtifactRequestById(int id) {
		this.jdbcTemplate.update("update requirementartifactrequest set status='deleted' where id=?", new Object[]{id}, new int[]{java.sql.Types.INTEGER});
	}

	public RequirementArtifactRequest getRequirementArtifactRequestById(int id) {
		return (RequirementArtifactRequest) this.jdbcTemplate.queryForObject("select* from requirementartifactrequest where id=? and status!='deleted'", 
				new Object[]{id}, new RequirementArtifactRequestRowMapper());
	}

	@SuppressWarnings("unchecked")
	public List<RequirementArtifactRequest> getRequirementArtifactRequestsByPId(int pid) {
		return this.jdbcTemplate.query("select * from requirementartifactrequest where pid=? and status!='deleted'", 
				new Object[]{pid}, new int[]{java.sql.Types.INTEGER}, new RequirementArtifactRequestRowMapper());
	}

	public void insertRequirementArtifactRequest(RequirementArtifactRequest newRequest) {
		jdbcTemplate.update("insert into requirementartifactrequest(pid,title,authorid,version,priority,source,rational,createdtime,updaterid,updatedtime,status,description,cost,effort,risk,updatedtimes,requirementtype,requirementartifactid,requesttype) " +
				"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", 
		new Object[]{
			newRequest.getPid(),
			newRequest.getTitle(),
			newRequest.getAuthorId(),
			newRequest.getVersion(),
			newRequest.getPriority(),
			newRequest.getSource(),
			newRequest.getRational(),
			newRequest.getCreatedtime(),
			newRequest.getUpdateUserId(),
			newRequest.getUpdatedtime(),
			newRequest.getStatus(),
			newRequest.getDescription(),
			newRequest.getCost(),
			newRequest.getEffort(),
			newRequest.getRisk(),
			newRequest.getUpdatedtimes(),
			newRequest.getRequirementtype(),
			newRequest.getRequirementartifactid(),
			newRequest.getRequesttype()
		},new int[] {
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.TIMESTAMP,
				java.sql.Types.INTEGER,
				java.sql.Types.TIMESTAMP,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR
		});
	}

}
